package model;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

import model.environment.Items;

public class ShoppingBasket {
	private HashMap<Integer, Integer> items;
	
	/**
	 * Creates a new shopping basket.
	 */
	public ShoppingBasket() {
		items = new HashMap<Integer, Integer>();
	}
	
	/**
	 * Gets the number of unique items in the shopping basket.
	 * @return The number of unique items in the shopping basket.
	 */
	public int getItemsInBasket()
	{
		return items.size();
	}
	
	/**
	 * Gets the combined value of the items in this basket.
	 * @return The price of the items in the basket.
	 */
	public int getBasketValue() {
		int result = 0;
				
		for (Map.Entry<Integer, Integer> entry : items.entrySet()) {
			Item item = Items.getItem(entry.getKey());
			
			int amount = entry.getValue();
			
			if (item == null)
				continue;
			
			result += item.getItemPrice() * amount;
		}
		
		return result;
	}
	
	/**
	 * Adds an item to the basket. If the item already exists, adds the new item amount to the old item amount.
	 * @param itemID The item to add.
	 * @param relativeAmount The amount of items to add to the basket.
	 */
	public void add(int itemID, int relativeAmount) {
		int value = relativeAmount;
		
		// If the item exists, add the existing amount to the new amount.
		if (items.containsKey(itemID)) {
			value += items.get(itemID);
		}
		
		items.put(itemID, value);
	}
	
	/**
	 * Sets an items amount to an absolute integer. If the item already exists, replaces the existing amount.
	 * If the absolute integer is 0 (zero), the item is deleted from the basket.
	 * @param itemID The item.
	 * @param absoluteAmount The absolute amount of items in the basket.
	 */
	public void set(int itemID, int absoluteAmount) {
		// Delete the item if there is to be 0 of them in the basket.
		if (absoluteAmount == 0) {
			items.remove(itemID);
			return;
		}
		
		// If item exists put() will replace the old value.
		items.put(itemID, absoluteAmount);
	}
	
	/**
	 * Gets the basket as a HashMap<Integer, Integer> (itemID, item amount).
	 * READ ONLY!!!!
	 * @return A HashMap<Integer, Integer>
	 */
	@SuppressWarnings("unchecked")
	public HashMap<Integer, Integer> getHashMap() {
		return (HashMap<Integer, Integer>) items.clone();
	}
	
	/**
	 * Gets a session's shopping basket or creates a new if none exsists.
	 * 
	 * REMEMBER TO SETSHOPPINGBASKET() WHEN YOU CHANGE THE BASKET!
	 * @return The session's shopping basket.
	 */
	public static ShoppingBasket getShoppingBasket(HttpSession session)
	{
		ShoppingBasket basket = (ShoppingBasket) session.getAttribute("ShoppingBasket");
		
		if (basket == null)
		{
			basket = new ShoppingBasket();
			session.setAttribute("ShoppingBasket", basket);
		}
		
		return basket;
	}
	
	/**
	 * Sets the sessions shopping basket.
	 * @param newBasket The sessions basket.
	 */
	public static void setShoppingBasket(ShoppingBasket newBasket, HttpSession session) {
		session.setAttribute("ShoppingBasket", newBasket);
	}
}
